import { notification } from "@wind/wind-ui";
import { useMemo } from "react";
import { DATATYPE, KEYS, STATUS } from "./types";
/**
 * @description 将某一位取反
 * @author hcluo
 * @date 13/11/2020
 * @param {*} source
 * @param {*} FLAG
 * @return {*}
 */
const toggle = (source, FLAG) => {
  let cur = source & FLAG;
  return cur ? (cur &= ~FLAG) : (cur |= FLAG);
};

/**
 * @description 按住shift后的处理事件
 * @author hcluo
 * @date 11/11/2020
 * @param {*} e
 * @param {*} app
 * @param {*} selected
 */
const shiftHandle = (e, app) => {
  let [x, y] = e.target.dataset?.position.split(",");
  let [cX, cY] = app.ctx.currentSelected;
  if ([x, y, cX, cY].every(i => i !== undefined)) {
    clearSelected(app.ctx.dataSource);

    let [startX, startY, endX, endY] = [
      Math.min(x, cX),
      Math.min(y, cY),
      Math.max(x, cX),
      Math.max(y, cY)
    ];

    for (let printY = startY; printY <= endY; printY++) {
      for (let printX = startX; printX <= endX; printX++) {
        app.ctx.dataSource[printY][printX].status = STATUS.selected;
      }
    }
  }
};

/**
 * @description 清空所有选择
 * @author hcluo
 * @date 11/11/2020
 * @param {*} dataSource
 */
const clearSelected = dataSource => {
  for (let y = 0; y < dataSource.length; y++) {
    for (let x = 0; x < dataSource?.[0].length; x++) {
      dataSource[y][x].status = STATUS.zero;
    }
  }
};

/**
 * @description 按住control后的处理事件
 * @author hcluo
 * @date 11/11/2020
 * @param {*} e
 * @param {*} app
 */
const selectRowOrCol = (app, type = 0) => {
  let [x, y] = app.ctx.currentSelected;

  if (type === 0) {
    for (let cX = 0; cX < app.ctx.dataSource?.[0].length; cX++) {
      app.ctx.dataSource[y][cX].status = toggle(
        app.ctx.dataSource[y][cX].status,
        STATUS.selected
      );
    }
  } else if (type === 1) {
    for (let cY = 0; cY < app.ctx.dataSource?.length; cY++) {
      app.ctx.dataSource[cY][x].status = toggle(
        app.ctx.dataSource[cY][x].status,
        STATUS.selected
      );
    }
  }
};

const deleteRowOrCol = (app, type = 0) => {
  let [x, y] = app.ctx.currentSelected;
  debugger;
  if (type === 0) {
    for (let cX = 0; cX < app.ctx.dataSource?.[0].length; cX++) {
      app.ctx.dataSource[y].splice(x, 1);
    }
  } else if (type === 1) {
    app.ctx.dataSource.splice(y, 1);
  }
};

/**
 * @description
 * @author hcluo
 * @date 13/11/2020
 * @param {*} e
 * @param {*} app
 * @param {number} [sign=1] 1为降序，-1为升序
 */
const sortTypes = ["desc", "asc", ""];
const sortByCol = (e, app) => {
  let [x, y] = e.target.dataset?.position?.split?.(",");
  let type = [x, y].indexOf(undefined);
  if (type === 0) {
  } else if (type === 1) {
    let sortType = app.ctx.viewInfo.header?.[x]?.sortType ?? "";
    let sign = sortTypes.indexOf(sortType) - 1;

    for (let i = 0; i < app.ctx.viewInfo.header.length; i++) {
      app.ctx.viewInfo.header[i].sortType = "";
    }

    app.ctx.viewInfo.header[x].sortType = sortTypes[(sign + 2) % 3];

    app.ctx.dataSource.sort((a, b) => {
      return a[x].data > b[x].data ? -1 * sign : 1 * sign;
    });
  }
};

/**
 * @description 点击选中当前
 * @author hcluo
 * @date 13/11/2020
 * @param {*} e
 * @param {*} app
 */
const selectCurrent = (e, app) => {
  let [x, y] = e.target.dataset?.position.split(",");
  if ([x, y].every(i => i !== undefined)) {
    app.ctx.dataSource[y][x].status = STATUS.current;
    app.ctx.currentSelected = [x, y];
  }
};

/**
 * @description 按住control后的处理事件
 * @author hcluo
 * @date 11/11/2020
 * @param {*} e
 * @param {*} app
 */
const controlHandle = (e, app) => {
  let [x, y] = e.target.dataset?.position.split(",");
  if ([x, y].every(i => i !== undefined)) {
    app.ctx.dataSource[y][x].status = toggle(
      app.ctx.dataSource[y][x].status,
      STATUS.selected
    );
  }
};

export const useItemHandle = () => app => {
  const itemHandle = useMemo(() => {
    return {
      onSelect: e => {
        switch (app.ctx.keyPress & (KEYS.zero | KEYS.Control | KEYS.Shift)) {
          case KEYS.Control:
            controlHandle(e, app);
            break;

          case KEYS.Shift:
            shiftHandle(e, app);
            break;
          default:
            clearSelected(app.ctx.dataSource);
            selectCurrent(e, app);
            break;
        }
        if (app.ctx.showMenu) {
          app.ctx.showMenu = false
        }
        app.actions.getViewData();
      },

      onDoubleClick: e => {
        let [x, y] = e.target.dataset?.position.split(",");
        if ([x, y].every(i => i !== undefined)) {
          app.ctx.dataSource[y][x].status = STATUS.editor;
          app.actions.getViewData();
        }
      },

      setItemValue: e => {
        let [x, y] = e.target.dataset?.position.split(",");
        if ([x, y].every(i => i !== undefined)) {
          app.ctx.dataSource[y][x].data = e.target.value;
          app.actions.getViewData();
        }
      },

      onHeaderClick: e => {
        sortByCol(e, app);
        app.actions.getViewData();
      },

      setDataType: (index=0) => {
        app.ctx.viewInfo.header[index].dataType = {
          type: DATATYPE.number,
          param: 2
        };
        app.actions.getViewData();
      },

      setTimeDate: (index=0) => {
        app.ctx.viewInfo.header[index].dataType = {
          type: DATATYPE.time,
          param: "yyyy-MM-dd"
        };
        app.actions.getViewData();
      },

      selectRowOrCol: t => {
        selectRowOrCol(app, t);
        app.actions.getViewData();
      },

      deleteRowOrCol: t => {
        deleteRowOrCol(app, t);
        app.actions.getViewData();
      },

      setAlign: (t,index = 0) => {
        app.ctx.viewInfo.header[index].dataAlign = t;
        app.actions.getViewData();
      },

      copySelect: () => {
        let select = [];
        for (let y = 0; y < app.ctx.dataSource.length; y++) {
          for (let x = 0; x < app.ctx.dataSource?.[0].length; x++) {
            if (app.ctx.dataSource[y][x].status === STATUS.selected)
              select.push(app.ctx.dataSource[y][x].data);
          }
        }
        notification.open({
          message: "复制选中",
          description: JSON.stringify(select)
        });
      }
    };
  }, []);

  app.actions.itemHandle = itemHandle
  
  return {
    props: ctx => ({
      itemHandle
    })
  };
};
